﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    public partial class ScrollingTextBox : TextBox
    {
        //TODO: Move to CC.Controls

        #region Constructor
        public ScrollingTextBox()
        {
            InitializeComponent();

            base.AcceptsReturn = false;
            base.AcceptsTab = false;
            base.ContextMenuStrip = _ContextMenuStripClear;
            base.Multiline = true;
            base.ScrollBars = ScrollBars.Both;
            base.TabStop = false;
        }
        #endregion

        #region Public Properties
        // NOTE: Not sure if hiding the setters is the best idea
        // ReSharper disable ValueParameterNotUsed
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool AcceptsReturn
        {
            get { return true; }
            //set { }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool AcceptsTab
        {
            get { return true; }
            //set { }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool Multiline
        {
            get { return true; }
            //set { }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool ReadOnly
        {
            get { return true; }
            //set { }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ScrollBars ScrollBars
        {
            get { return ScrollBars.Both; }
            //set { }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool TabStop
        {
            get { return false; }
            //set { }
        }
        // ReSharper restore ValueParameterNotUsed
        #endregion

        #region Private Event Methods
        private void _ToolStripMenuItemClear_Click(object sender, EventArgs e)
        {
            Clear();
        }
        #endregion

        #region Protected Methods
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Tab)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
            }

            base.OnKeyDown(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            SelectionStart = TextLength;
            SelectionLength = 0;
            ScrollToCaret();

            base.OnTextChanged(e);
        }
        #endregion
    }
}
